from zope.interface import implements
import os, sys
import shutil
import re

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

	
from watcher.interfaces import ITestBehavior
from watcher.decision import TestBehavior
from watcher.exceptions import WatcherActionError



class TemplateBehavior(TestBehavior):
    """template run behavior"""
    implements(ITestBehavior)
    

class AlwaysTrue(TestBehavior):
    """bool to always return true in the case that we want
       to have certain branches always on"""
    def __call__(self, workItem, data):
        return True


class AlwaysFalse(TestBehavior):
    """bool to always return false in the case that we want to
       disable a specific feature"""
    def __call__(self, workItem, data):
        return False


class FilenameMatch(TestBehavior):
    """match a regexp in filename"""
    def __call__(self, workItem, data):
        filename = os.path.basename(workItem)
        p = data.get('pattern')
        ignorecase = data.get('ignorecase')
        if ignorecase.lower() == "y":
            matcher = re.compile(p, re.IGNORECASE)
        else:
            matcher = re.compile(p)
        match = matcher.search(filename)
        if match:
            return True
        return False

